package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.GroupUserRequest;
import com.xh.bussiness.system.constant.UserDataCacheConstant;
import com.xh.bussiness.system.model.Group;
import com.xh.bussiness.system.model.GroupUser;
import com.xh.bussiness.system.dao.GroupUserMapper;
import com.xh.bussiness.system.service.IGroupUserService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.CacheUtils;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户与组关系表 服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-11-23
 */
@Service
public class GroupUserServiceImpl extends BaseServiceImpl<GroupUserMapper, GroupUser> implements IGroupUserService {
    @Override
    public XhPage queryGroupUserPage(XhPage page, GroupUserRequest bean) {
        QueryWrapper qw = new QueryWrapper<GroupUser>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page, qw);
    }

    @Override
    public List queryGroupUser(GroupUserRequest bean) {
        QueryWrapper qw = new QueryWrapper<GroupUser>();
        //拼装条件
        qw = prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public List<GroupUser> queryGroupUserByGroupId(String groupId) {
        QueryWrapper<GroupUser> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId);
        return this.dao.selectList(qw);
    }

    @Override
    public Integer queryGroupUserCountByGroupId(String groupId) {
        QueryWrapper<GroupUser> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId);
        return this.dao.selectCount(qw);
    }

    @Override
    public List<GroupUser> queryGroupUserByUserId(String userId) {
        QueryWrapper<GroupUser> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        return this.dao.selectList(qw);
    }

    @Override
    public boolean deleteByUserId(String userId, String groupId) {
        boolean result = this.dao.deleteByUserId(userId, groupId);
        CacheUtils.delete(UserDataCacheConstant.USER_DATA_VERSIN_KEY_PREFIX + userId);
        return result;
    }

    @Override
    public boolean insert(GroupUser groupUser) {
        boolean result = super.insert(groupUser);
        CacheUtils.delete(UserDataCacheConstant.USER_DATA_VERSIN_KEY_PREFIX + groupUser.getUserId());
        return result;
    }

    @Override
    public boolean update(GroupUser groupUser) {
        boolean result = super.update(groupUser);
        CacheUtils.delete(UserDataCacheConstant.USER_DATA_VERSIN_KEY_PREFIX + groupUser.getUserId());
        return result;
    }

    @Override
    public GroupUser queryGroupUerByUserIdAndGroupId(String userId, String groupId) {
        QueryWrapper<GroupUser> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId);
        qw.eq("user_id", userId);

        List<GroupUser> groupUsers = this.dao.selectList(qw);
        if (CollectionUtils.isEmpty(groupUsers)) {
            return null;
        }
        return groupUsers.get(0);
    }

    @Override
    public List<Group> queryAllGroupByUserId(String userId) {
        return this.dao.queryUserGroupByUserId(userId);
    }

    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(GroupUserRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getGroupId())) {
            qw.eq("group_id", bean.getGroupId());
        }
        if (!StringUtils.isEmpty(bean.getUserId())) {
            qw.eq("user_id", bean.getUserId());
        }
        return qw;
    }

    /**
     * 查询当前用户的学科组
     * @author 赵文博
     * @date 2021/6/22 10:33
     * @param userId
     * @return java.util.List<com.xh.bussiness.system.model.Group>
     **/
    @Override
    public List<Group> queryGroupByUserId(String userId) {
        return this.dao.queryGroupByUserId(userId);
    }
}
